//
//  WLUploadedTaskCell.m
//  WanLian
//
//  Created by 123456 on 2018/12/8.
//  Copyright © 2018 wanlian. All rights reserved.
//

#import "WLUpDownloadedTaskCell.h"
#import "NSString+Ext.h"
#import "WLFileUpDownLoadModel.h"
#import "WLFileHandle.h"
#import "HXPhotoTools.h"
#import <Photos/Photos.h>
#import "WLSuportFileFormat.h"
#import "UIImage+ext.h"
@interface WLUpDownloadedTaskCell ()

@property (weak, nonatomic) IBOutlet UIImageView *headImageView;
@property (weak, nonatomic) IBOutlet UILabel *fileNameLable;
@property (weak, nonatomic) IBOutlet UILabel *completeTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *fileSizeLabel;
@property (assign, nonatomic) PHImageRequestID requestID;

@end
@implementation WLUpDownloadedTaskCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.headImageView.contentMode = UIViewContentModeScaleAspectFill;
    self.headImageView.clipsToBounds = YES;
}

- (void)setModel:(WLFileUpDownLoadModel *)model {
    _model = model;
    [self cancelRequest];
    self.fileNameLable.text = model.fileName;
    self.completeTimeLabel.text = model.comleteTime;
    self.fileSizeLabel.text = model.fileTotalSize;

    if (model.fileType == SPFileTypePhoto) {
        if (![WLSuportFileFormat photoSupportFormat:model.fileName]) {
            self.headImageView.image = [UIImage imageNamed:@"CloudImageNotSupport"];
        } else {
            
            WLWkSelf
            HXPhotoModel *photoModel = [[HXPhotoModel alloc] init];
            if (model.localIdentifier) {
                NSArray *localIdentifiers = @[model.localIdentifier];
                PHAsset *asset = [[PHAsset fetchAssetsWithLocalIdentifiers:localIdentifiers options:nil] firstObject];
                photoModel.asset = asset;
                photoModel.requestSize = self.headImageView.frame.size;
                PHImageRequestID requestID = [HXPhotoTools getPhotoForLocalIdentifier:model.localIdentifier size:self.headImageView.frame.size completion:^(UIImage *image, NSDictionary *info) {
                    if (image) {
                        weakSelf.headImageView.image = image;
                    } else {
                        
                        weakSelf.headImageView.image = [UIImage imageNamed:@"CloudImadgeLoadFail"];
                    }
                }];
                self.requestID = requestID;
            } else if (model.fileURL) {
                WLWkSelf
                [UIImage compressImageWithURL:model.fileURL rate:0.2 completeBlock:^(UIImage * _Nonnull image) {
                    if (image) {
                        weakSelf.headImageView.image = image;
                    } else {
                        weakSelf.headImageView.image = [UIImage imageNamed:@"CloudImadgeLoadFail"];
                    }
                }];
            }
        }
//        self.headImageView.image = [UIImage imageWithData:model.fileData];
    } else if (model.fileType == SPFileTypeVideo) {
        WLWkSelf
        if (model.mediaLocalURL) {
            [WLFileHandle thumbnailImageForVideo:model.mediaLocalURL atTime:0.0 completeBlock:^(UIImage * _Nonnull image) {
                [weakSelf configWithIconImage:image];
            }];
        } else if (model.fileURL) {
            [WLFileHandle thumbnailImageForVideo:model.fileURL atTime:0.0 completeBlock:^(UIImage * _Nonnull image) {
                [weakSelf configWithIconImage:image];
            }];
        }
        
    }
    
}
- (void)configWithIconImage:(UIImage *)image {
    if (image) {
        self.headImageView.image = image;
    } else {
        self.headImageView.image = [UIImage imageNamed:@"video_placeholder"];
    }
}
- (void)dealloc {
    [self cancelRequest];
}
- (void)cancelRequest {
    if (self.requestID) {
        [[PHImageManager defaultManager] cancelImageRequest:self.requestID];
        self.requestID = -1;
    }
}

@end
